package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventDzbg;
import com.ruoyi.event.domain.bo.EventDzbgBo;
import com.ruoyi.event.domain.vo.EventDzbgVo;
import com.ruoyi.event.mapper.EventDzbgMapper;
import com.ruoyi.event.service.IEventDzbgService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 地址变更Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventDzbgServiceImpl implements IEventDzbgService {

    private final EventDzbgMapper baseMapper;

    /**
     * 查询地址变更
     */
    @Override
    public EventDzbgVo queryById(Long dzbgId) {
        return baseMapper.selectVoById(dzbgId);
    }

    /**
     * 查询地址变更列表
     */
    @Override
    public TableDataInfo<EventDzbgVo> queryPageList(EventDzbgBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventDzbg> lqw = buildQueryWrapper(bo);
        Page<EventDzbgVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地址变更列表
     */
    @Override
    public List<EventDzbgVo> queryList(EventDzbgBo bo) {
        LambdaQueryWrapper<EventDzbg> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventDzbg> buildQueryWrapper(EventDzbgBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventDzbg> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventDzbg::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getBgyy()), EventDzbg::getBgyy, bo.getBgyy());
        lqw.eq(StringUtils.isNotBlank(bo.getXzxd()), EventDzbg::getXzxd, bo.getXzxd());
        lqw.eq(StringUtils.isNotBlank(bo.getBghdz()), EventDzbg::getBghdz, bo.getBghdz());
        lqw.eq(bo.getJdkfqssj() != null, EventDzbg::getJdkfqssj, bo.getJdkfqssj());
        lqw.eq(bo.getJdkfjssj() != null, EventDzbg::getJdkfjssj, bo.getJdkfjssj());
        lqw.eq(StringUtils.isNotBlank(bo.getBgyyfj()), EventDzbg::getBgyyfj, bo.getBgyyfj());
        lqw.eq(StringUtils.isNotBlank(bo.getJsdgzbmyj()), EventDzbg::getJsdgzbmyj, bo.getJsdgzbmyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJsdgzbmqm()), EventDzbg::getJsdgzbmqm, bo.getJsdgzbmqm());
        lqw.eq(bo.getJsdgzbmrq() != null, EventDzbg::getJsdgzbmrq, bo.getJsdgzbmrq());
        lqw.eq(StringUtils.isNotBlank(bo.getJsdgzxzyj()), EventDzbg::getJsdgzxzyj, bo.getJsdgzxzyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJsdgzxzqm()), EventDzbg::getJsdgzxzqm, bo.getJsdgzxzqm());
        lqw.eq(bo.getJsdgzxzrq() != null, EventDzbg::getJsdgzxzrq, bo.getJsdgzxzrq());
        lqw.eq(StringUtils.isNotBlank(bo.getZxdgzzyj()), EventDzbg::getZxdgzzyj, bo.getZxdgzzyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZxdgzzqm()), EventDzbg::getZxdgzzqm, bo.getZxdgzzqm());
        lqw.eq(bo.getZxdgzzrq() != null, EventDzbg::getZxdgzzrq, bo.getZxdgzzrq());
        lqw.eq(StringUtils.isNotBlank(bo.getZxdldxzyj()), EventDzbg::getZxdldxzyj, bo.getZxdldxzyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZxdldxzqm()), EventDzbg::getZxdldxzqm, bo.getZxdldxzqm());
        lqw.eq(bo.getZxdldxzrq() != null, EventDzbg::getZxdldxzrq, bo.getZxdldxzrq());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EventDzbg::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增地址变更
     */
    @Override
    public Boolean insertByBo(EventDzbgBo bo) {
        EventDzbg add = BeanUtil.toBean(bo, EventDzbg.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDzbgId(add.getDzbgId());
        }
        return flag;
    }

    /**
     * 修改地址变更
     */
    @Override
    public Boolean updateByBo(EventDzbgBo bo) {
        EventDzbg update = BeanUtil.toBean(bo, EventDzbg.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventDzbg entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地址变更
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
